import { Component, OnInit, AfterViewInit } from '@angular/core'
import { FormGroup, FormBuilder, Validators } from '@angular/forms'
import { NzMessageService } from 'ng-zorro-antd'
import { SettingsService } from '@delon/theme'

import { Merchant, ICreateMerchant } from '@shared/interfaces/merchant'
import { NewPageBaseClass } from '@shared/classes/NewPageBaseClass'
import { MerchantStoreService } from '../merchant-store.service'
import { ERoleType, EUserType } from '@shared/enums'
import { IAamsItem } from '@shared/components/aams-select/aams-select.interface'
import { getAAMSAcquirer, getAAMSGroup, acquirerEditPermission, agentEditPermission } from 'app/utils/helpers'
import { User } from '@shared/interfaces/user'
import { I18NService } from '@core'

@Component({
  selector: 'app-merchant-new-system',
  templateUrl: './system.component.html',
  styleUrls: ['../../../../shared/styles/new-page-form.less'],
})
export class MerchantNewSystemComponent extends NewPageBaseClass implements OnInit {
  merchant: Merchant
  validateForm: FormGroup
  eRoleType = ERoleType

  chosenAcquirer: IAamsItem = null
  chosenGroup: IAamsItem = null

  constructor(
    public msg: NzMessageService,
    public i18n: I18NService,
    private fb: FormBuilder,
    private merchantStore: MerchantStoreService,
    public settings: SettingsService,
  ) {
    super(msg, i18n)
  }

  ngOnInit(): void {
    this.merchantStore.value$.subscribe(createMerchant => {
      this.merchant = createMerchant.merchant
      const authUser = this.settings.user as User
      const { common: merchantCommon } = this.merchant
      if (merchantCommon.insCode) {
        this.chosenAcquirer = {
          num: merchantCommon.insCode,
          code: merchantCommon.insCode,
          name: merchantCommon.insName,
          nameEN: merchantCommon.insNameEN
        }

        this.chosenGroup = {
          num: merchantCommon.agentCode,
          code: merchantCommon.agentCode,
          name: merchantCommon.agentName,
          nameEN: merchantCommon.agentNameEN
        }
      } else {
        this.chosenAcquirer = getAAMSAcquirer(authUser)
        this.chosenGroup = getAAMSGroup(authUser)
      }

      this.validateForm = this.fb.group({
        institution: [{
          value: this.chosenAcquirer.code ? this.chosenAcquirer : null,
          disabled: acquirerEditPermission(authUser)
        }, [ Validators.required ]],
        insName: [ this.chosenAcquirer.name ],
        insNameEN: [ this.chosenAcquirer.nameEN ],
        // Disable agent field when userType is agent.
        agent: [{
          value: this.chosenGroup.code ? this.chosenGroup : null,
          disabled: agentEditPermission(authUser)
        }, [ ]],
        agentName: [ this.chosenGroup.name ],
        agentNameEN: [ this.chosenGroup.nameEN ],
      })
    })
  }

  checkValid(): boolean {
    if (this.validateForm.valid) {
      this.fullfilData()
      this.merchantStore.updateValue({ merchant: this.merchant } as ICreateMerchant)
      this.msg.success(this.trans('GENERAL.NOTIFICATION.VALIDATION_SUCCESS'))
      return true
    } else {
      this.makeAllControlDirty(this.validateForm)
      this.msg.error(this.trans('GENERAL.NOTIFICATION.VALIDATION_FAIL'))
      return false
    }
  }

  handleChosenBusiness(business, eRoleType): void {
    if (!business) {
      business = {}
    }

    if (eRoleType === ERoleType.Acquirer) {
      this.validateForm.get('insName').setValue(business.name)
      this.validateForm.get('insNameEN').setValue(business.nameEN)
      this.chosenAcquirer = business
    } else if (eRoleType === ERoleType.Group) {
      this.validateForm.get('agentName').setValue(business.name)
      this.validateForm.get('agentNameEN').setValue(business.nameEN)
      this.chosenGroup = business
    }
  }

  pushDataToStore(): void {
    const merchant = this.merchantStore.value.merchant
    Object.assign(merchant, this.merchant)
  }

  private fullfilData(): void {
    const { common } = this.merchant
    common.insCode = this.chosenAcquirer.code
    common.insName = this.chosenAcquirer.name
    common.insNameEN = this.chosenAcquirer.nameEN

    // agent is optional
    common.agentCode = this.chosenGroup.code
    common.agentName = this.chosenGroup.name
    common.agentNameEN = this.chosenGroup.nameEN

    this.merchant.common = common
  }
}
